package com.cs.study.homework._0318.demo;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


public class TraderStream {
    public static void main(String[] args) {
        List<Transaction> transactions = new TransactionRecord().transactions;
        // 找出2011年发生的交易记录, 按照从低到高交易额排序
        List<Transaction> list = transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted((r1, r2) -> r2.getValue() - r1.getValue())
                .collect(Collectors.toList());
        System.out.println(list);

        // 找出交易员都在哪些不同的城市工作过的城市
        List<String> city = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .distinct()
                .toList();
        System.out.println(city);

        // 查找来自北京的交易, 按交易员姓名排序
        List<Transaction> trade = transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity() == "beijing")
                .sorted((n1, n2) -> n1.getTrader().getName().hashCode() - n2.getTrader().getName().hashCode())
                .collect(Collectors.toList());
        System.out.println(trade);

        // 返回一个字符串包含所有交易员, 字符串的顺序按照姓名排序
        List<String> list2 = transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct().sorted()
                .collect(Collectors.toList());
        System.out.println(list2);

        // 判断有没有交易员在北京工作
        boolean b = transactions.stream()
                .anyMatch(transaction -> transaction.getTrader().getCity() == "beijing");
        System.out.println(b);

        // 打印所有发生在北京的交易记录金额
        List<Integer> list3 = transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity() == "beijing")
                .map(Transaction::getValue)
                .collect(Collectors.toList());
        System.out.println(list3);

        // 所有交易中最高的交易是多少
        Optional<Integer> list4 = transactions.stream()
                .sorted((v1, v2) -> v2.getValue() - v1.getValue())
                .map(Transaction::getValue)
                .findFirst();
        System.out.println(list4);

        // 所有交易中, 交易最低的交易信息
        Optional<Transaction> low = transactions.stream()
                .sorted((v1, v2) -> v1.getValue() - v2.getValue()).findFirst();
        System.out.println(low.get());


    }
}
